package com.geekgo.lucenepp.core.config.index.impl;

import com.geekgo.lucenepp.core.config.datasource.LuceneDataSource;
import com.geekgo.lucenepp.core.config.index.IndexFactory;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 删除索引
 *
 * @author HeHaoyang
 * @create 2017-06-14 11:14
 **/
public class DeleteIndex implements IndexFactory {

    public final Logger logger = LoggerFactory.getLogger(DeleteIndex.class);

    @Override
    public boolean delAll(LuceneDataSource dataSource) {
        boolean flag =true;
        IndexWriter indexWriter = dataSource.getIndexWriter();
        try {
            indexWriter.deleteAll();
            indexWriter.commit();
        } catch (IOException e) {
            flag = false;
            logger.error("清空索引失败:",e);
        }
        return flag;
    }

    @Override
    public boolean deleteQuery(LuceneDataSource dataSource, Query... query) {
        boolean flag = true;
        IndexWriter indexWriter = dataSource.getIndexWriter();
        try {
            indexWriter.deleteDocuments(query);
            indexWriter.commit();
        } catch (IOException e) {
            flag = false;
            logger.error("删除索引失败:",e);
        }
        return flag;
    }
}
